package br.com.netomarin.bundesliga.model;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.jdo.JDOHelper;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import br.com.netomarin.bundesliga.commons.UtilCommons;
import br.com.netomarin.bundesliga.persistence.Match;
import br.com.netomarin.bundesliga.persistence.PMF;

public class MatchModel {

	private MatchModel() {
	}

	public static int saveMatches(ArrayList<Match> matches) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		// Transaction tx = pm.currentTransaction();

		try {
			// tx.begin();
			// pm.makeTransactionalAll(matches);
			// tx.commit();
			pm.makePersistentAll(matches);
		} finally {
			// if (tx.isActive()) {
			// tx.rollback();
			// }
			pm.close();
		}

		return matches.size();
	}

	public static void saveMatch(Match match) {
		PersistenceManager pm = JDOHelper.getPersistenceManager(match);
		if (pm == null)
			pm = PMF.get().getPersistenceManager();

		try {
			pm.makePersistent(match);
		} finally {
			pm.close();
		}
	}

	public static List<Match> loadAllMatches() {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query q = pm.newQuery(Match.class);
		List<Match> matches = (List<Match>) q.execute();
		return matches;
	}

	public static Match loadMatchFromCSV(String line) {
		Match match = new Match();
		StringTokenizer st = new StringTokenizer(line, ",");
		for (int i = 0; st.hasMoreTokens(); i++) {
			String token = st.nextToken();
			switch (i) {
			case 0:
				match.setHomeTeam(token);
				break;
			case 1:
				match.setHomeP1(token);
				break;
			case 2:
				match.setHomeP2(token);
				break;
			case 3:
				match.setScoreHomeTeam(Byte.parseByte(token));
				break;
			case 4:
				match.setVisitorTeam(token);
				break;
			case 5:
				match.setVisitorP1(token);
				break;
			case 6:
				match.setVisitorP2(token);
				break;
			case 7:
				match.setScoreVisitorTeam(Byte.parseByte(token));
				break;
			case 8:
				match.setMatchType(token);
				break;
			default:
				break;
			}
		}

		match.setConfirmed(true);
		return match;
	}

	public static boolean notifyMatch(Match m) {
		saveMatch(m);
		UtilCommons.sendMatchEmail(m);

		return true;
	}

	public static Match loadMatchByKey(String matchKey) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Match m = null;
		try {
			m = pm.getObjectById(Match.class, matchKey);
		} catch (JDOObjectNotFoundException e) {}

		return m;
	}
	
	public static boolean confirmMatch(Match m) {
		m.setConfirmed(true);
		saveMatch(m);
		return true;
	}

	public static boolean challengeMatch(Match m) {
		saveMatch(m);
		UtilCommons.sendChallengedMatchEmail(m);
		return true;
	}
	
	public static boolean deleteMatch(String key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Match m = pm.getObjectById(Match.class, key);
		pm.deletePersistent(m);
		
		return true;
	}

	public static void escalateMatch(String match) {
		Match m = loadMatchByKey(match);
		Match cm = loadMatchByKey(m.getChallengedMatch());
		m.setEscalated(true);
		cm.setEscalated(true);
		saveMatch(m);
		saveMatch(cm);
		UtilCommons.sendEscalatedMatchEmail(m);
	}
}